package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Notifications;
import com.Data.User;
import com.Managers.LogManagerClass;
import com.Managers.UserManagerClass;


public class SoapNotifications {

    private static final String TAG = "SOAP NOTIFICATIONS";

    SoapSerializationEnvelope envelope;

    //
    PropertyInfo pi;

    SoapObject Request;

    public String addNotification(String[] data) {

        String METHOD_NAME = "addNotifications";
        String SOAP_ACTION = "http://ShareUp.com/addNotifications";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "#";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            //
            return response.toString();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String trace = " ";
            for (StackTraceElement line : stack) {
                trace += line.toString();
            }
            Log.e(TAG, trace);
            LogManagerClass.writeLog(TAG + trace);

            return trace;
        }
        catch (IOException x) {
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());

            return "-10";
        }
        catch (XmlPullParserException x) {
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());

            return x.getMessage();
        }

    }

    public String deleteNotifications(int id) {

        String METHOD_NAME = "deleteNotifications";
        String SOAP_ACTION = "http://ShareUp.com/deleteNotifications";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", id);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            return response.toString();
        }
        catch (Exception x) {
            response = x.getMessage();
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());

            return x.getMessage();

        }

    }

    public Notifications[] getNotifications() {

        //
        String METHOD_NAME = "getNotifications";
        String SOAP_ACTION = "http://ShareUp.com/getNotifications";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", UserManagerClass.UserLogged.getID());
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);

        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (Exception e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG, Trace);
            LogManagerClass.writeLog(TAG + Trace);
        }

        return getNotificationsData(response);
    }

    public String getNotificationsCount() {

        String METHOD_NAME = "getNotificationCount";
        String SOAP_ACTION = "http://ShareUp.com/getNotificationCount";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", UserManagerClass.UserLogged.getID());
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            return response.toString();
        }
        catch (Exception x) {
            response = x.getMessage();
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());

            return x.getMessage();

        }
    }

    private Notifications[] getNotificationsData(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Notifications[] nots = new Notifications[jsonArray.length()];

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                Notifications not = new Notifications(0);
                User from = new User();

                // NOTIFICATION INFO
                int ID = Integer.parseInt(jo.getString("IDNotification"));
                int Type = jo.getInt("Type");
                String Date = jo.getString("Date");
                String Extras = jo.getString("ExtraData");
                // USER INFO
                String Owner = jsonArray.getJSONObject(i).getJSONObject("From").toString();
                jo = new JSONObject(Owner);
                int OwnerId = Integer.parseInt(jo.getString("ID"));
                String OwnerName = jo.getString("RealName");
                String OwnerProfile = jo.getString("ProfilePicture");
                String Cover = jo.getString("CoverPicture");

                // Setting
                from.setID(OwnerId);
                from.setRealName(OwnerName);
                from.setProfilePicture(OwnerProfile);
                from.setCoverPicture(Cover);
                //
                not.setFrom(from);
                not.setIdNotification(ID);
                not.setType(Type);
                not.setTo(UserManagerClass.UserLogged);
                not.setDate(Date);
                not.setExtra(Extras);
                nots[i] = not;

            }
            return nots;

        }
        catch (JSONException x) {
            Log.e(TAG, x.getMessage());
            LogManagerClass.writeLog(TAG + x.getMessage());
            return null;
        }

    }

}
